package com.dyz.swordFingerOffer;

import java.util.Stack;

/**
 * @author: daiyizheng
 * @date: 2021/6/2 13:10
 * @description:
 */
public class MinFunction30 {
    //创建两个栈对象，一个用于存储原栈数据，一个用于暂时辅助存储数据
    Stack<Integer> stack = new Stack<Integer>();
    Stack<Integer> subStack = new Stack<Integer>();


    public void push(int node) {
        stack.push(node);

    }

    //pop操返回栈顶元素的同时会remove栈顶元素
    public void pop() {
        int p = stack.pop();

    }

    //这里返回栈的顶部元素，但是不会remove栈顶元素
    public int top() {
        int topNumber = stack.peek();
        return topNumber;

    }

    public int min() {
        int minNumber = Integer.MAX_VALUE;
        //依次取出栈里面的数直到栈为空
        while(stack.isEmpty() != true){
            //得到栈顶元素
            int number = stack.pop();
            if(minNumber > number){
                minNumber = number;
            }
            //弹出一个，压入一个
            subStack.push(number);
        }
        //最后将辅助栈中的元素再重新压入原来的栈里
        while(subStack.isEmpty() != true){
            stack.push(subStack.pop());
        }

        return minNumber;

    }
}
